package org.jeecg.modules.zhima.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.jeecg.modules.zhima.center.entity.OmsCartItem;
import org.jeecg.modules.zhima.center.entity.UmsMember;
import org.jeecg.modules.zhima.center.service.IOmsCartItemService;
import org.jeecg.modules.zhima.portal.domain.CartPromotionItem;
import org.jeecg.modules.zhima.portal.domain.PmsPortalProductDetail;
import org.jeecg.modules.zhima.portal.service.ICartItemService;
import org.jeecg.modules.zhima.portal.service.IOmsPromotionService;
import org.jeecg.modules.zhima.portal.service.IProductService;
import org.jeecg.modules.zhima.portal.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartItemServiceImpl implements ICartItemService {

    @Autowired
    private IUserService userService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IOmsCartItemService cartItemService;
    @Autowired
    private IOmsPromotionService promotionService;

    @Override
    public OmsCartItem add(OmsCartItem cartItem) {

        UmsMember currentMember = userService.getCurrentMember();
        cartItem.setMemberId(currentMember.getId());
        cartItem.setMemberNickname(currentMember.getNickname());
        cartItem.setDeleteStatus(0);
        cartItem.setIsChecked(0);
        OmsCartItem existCartItem = cartItemService.getOne(cartItem.getProductId(), cartItem.getMemberId(), cartItem.getProductSkuId());
        if (existCartItem == null) {
            PmsPortalProductDetail pmsProduct = productService.getDetail(cartItem.getProductId());
            cartItem.setCreateDate(new Date());
            cartItem.setProductName(pmsProduct.getProduct().getName());
            cartItem.setProductPic(pmsProduct.getProduct().getPic());
            cartItem.setPrice(pmsProduct.getProduct().getPrice());
            cartItem.setProductSn(pmsProduct.getProduct().getProductSn());
            cartItem.setProductCategoryId(pmsProduct.getProduct().getProductCategoryId());
            cartItem.setProductBrand(pmsProduct.getBrand().getName());
            cartItem.setProductSubTitle(pmsProduct.getProduct().getSubTitle());
            cartItemService.saveOrUpdate(cartItem);
            return cartItem;
        } else {
            cartItem.setModifyDate(new Date());
            existCartItem.setQuantity(existCartItem.getQuantity() + cartItem.getQuantity());
            cartItemService.updateById(existCartItem);
            return existCartItem;
        }
    }

    @Override
    public List<CartPromotionItem> listPromotion(Integer memberId, List<Integer> cartIds) {
        List<OmsCartItem> cartItemList = cartItemService.listByMemberId(memberId);
        if(CollUtil.isNotEmpty(cartIds)){
            cartItemList = cartItemList.stream().filter(item->cartIds.contains(item.getId())).collect(Collectors.toList());
        }
        List<CartPromotionItem> cartPromotionItemList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(cartItemList)){
            cartPromotionItemList = promotionService.calcCartPromotion(cartItemList);
        }
        return cartPromotionItemList;
    }
}
